package com.hao.stack;

import java.math.BigDecimal;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/4/25 11:09
 */
public class Calculator {

    public Integer parseFormula(String formula){
        // 用于存储数字
        MyStackImpl<Integer> numStack = new MyStackImpl();
        // 用于存储运算符
        MyStackImpl<Character> operStack = new MyStackImpl();
        char[] formulaChar = formula.toCharArray();
        int index = 0;
        while (index < formulaChar.length){
            // 解析数字
            boolean isNum ;
            StringBuilder numStr = new StringBuilder();
            do{
                if (index >= formulaChar.length){
                    break;
                }
                char c = formulaChar[index];
                isNum = c >= '0' && c <= '9';
                if (isNum){
                    numStr.append(c);
                    index++;
                }
            }while(isNum);
            Integer num = Integer.valueOf(numStr.toString());
            numStack.push(num);
            if (index >= formulaChar.length){
                break;
            }
            // 解析运算符
            char oper = formulaChar[index];
            boolean isOper = oper == '+' || oper == '-' || oper == '*' || oper == '/';
            if (isOper){
                index++;
                if (operStack.size() == 0){
                    operStack.push(oper);
                }else {
                    char preOper = operStack.get();
                    boolean isHigh = (oper == '*' || oper == '/') && (preOper == '+' || preOper == '-');
                    if (isHigh){
                        operStack.push(oper);
                    }else {
                        int nextNum = numStack.pop();
                        int preNum = numStack.pop();
                        int result;
                        switch (preOper){
                            case '+':
                                result = preNum + nextNum;
                                break;
                            case '-':
                                result = preNum - nextNum;
                                break;
                            case '*':
                                result = preNum * nextNum;
                                break;
                            case '/':
                                result = preNum / nextNum;
                                break;
                            default:
                                result=0;
                        }
                        numStack.push(result);
                        operStack.pop();
                        operStack.push(oper);
                    }
                }
            }
        }
        int size = operStack.size();
        for (int i = 0; i < size ;i++) {
            char oper = operStack.pop();
            int nextNum = numStack.pop();
            int preNum = numStack.pop();
            int result;
            switch (oper){
                case '+':
                    result = preNum + nextNum;
                    break;
                case '-':
                    result = preNum - nextNum;
                    break;
                case '*':
                    result = preNum * nextNum;
                    break;
                case '/':
                    result = preNum / nextNum;
                    break;
                default:
                    result=0;
            }
            numStack.push(result);
        }
        return numStack.pop();
    };
}
